import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

public class TankClient extends Frame {

    public static final int GAME_WIDTH = 800;
    public static final int GAME_HEIGHT = 600;
    
    Tank myTank = new Tank(50, 50, true, this);
    Blood b = new Blood();
    List<Tank> tanks = new ArrayList<Tank>();
    Wall w1 = new Wall(250, 250, 250, 10, this);
    List<Missile> missiles = new ArrayList<Missile>();
    List<Explode> explodes = new ArrayList<Explode>();
    
    // Tambahan: level permainan
    private int level = 1;
    
    Image offScreenImage = null;
    
    public void paint(Graphics g) {
        // Tampilkan informasi status permainan
        g.setColor(Color.BLACK);
        g.drawString("Missiles Count: " + missiles.size(), 10, 50);
        g.drawString("Explosions Count: " + explodes.size(), 10, 70);
        g.drawString("Enemy Tanks: " + tanks.size(), 10, 90);
        g.drawString("MyTank Blood: " + myTank.getLife(), 10, 110);
        g.drawString("Level: " + level, 10, 130);
        
        
        // Gambar objek-objek game
        w1.draw(g);
        myTank.draw(g);
        b.draw(g);
        // Bonus darah, jika disentuh, darah kembali penuh
        myTank.eat(b);
        myTank.collidesWithWall(w1);
        myTank.collidesWithTanks(tanks);
        
        // Gambar enemy tank
        for (Tank t : tanks) {
            t.draw(g);
            t.collidesWithWall(w1);
            t.collidesWithTanks(tanks);
        }
        
        // Gambar missile dan cek collision
        for (int i = 0; i < missiles.size(); i++) {
            Missile m = missiles.get(i);
            m.hitTanks(tanks);
            m.hitTank(myTank);
            m.hitWall(w1);
            m.draw(g);
        }
        
        // Gambar efek explosion
        for (int i = 0; i < explodes.size(); i++) {
            Explode e = explodes.get(i);
            e.draw(g);
        }
        
        // Jika semua enemy telah hancur, naikkan level dan spawn enemy baru
        if (tanks.isEmpty()) {
            levelUp(g);
        }
    }
    
    /**
     * Metode untuk menangani transisi level
     */
    private void levelUp(Graphics g) {
        // Tampilkan pesan level cleared
        g.setColor(Color.RED);
        g.setFont(new Font("Arial", Font.BOLD, 36));
        g.drawString("LEVEL CLEARED!", GAME_WIDTH/2 - 150, GAME_HEIGHT/2);
        
        // Pause sejenak agar pemain melihat pesan tersebut
        try {
            Thread.sleep(1000);  // jeda 1 detik
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        
        // Naikkan level dan spawn enemy tanks baru
        level++;
        spawnEnemyTanks();
    }
    
    /**
     * Metode untuk memunculkan enemy tanks berdasarkan level
     */
    private void spawnEnemyTanks() {
        // Jumlah enemy meningkat seiring naiknya level.
        // Misal: pada level 1: 10 enemy, tiap level bertambah 2 enemy.
        int enemyCount = 10 + level * 2;
        tanks.clear();
        for (int i = 0; i < enemyCount; i++) {
            // Penempatan enemy secara sederhana (dapat disesuaikan untuk menghindari tumpang tindih)
            int x = 50 + (i * 40) % (GAME_WIDTH - Tank.WIDTH);
            int y = 50 + ((i * 40) / (GAME_WIDTH - Tank.WIDTH)) * 40;
            tanks.add(new Tank(x, y, false, this));
        }
    }
    
    /**
     * Override update untuk mengimplementasikan double-buffering
     */
    public void update(Graphics g) {
        if (offScreenImage == null) {
            offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
        }
        Graphics gOffScreen = offScreenImage.getGraphics();
        Color c = gOffScreen.getColor();
        gOffScreen.setColor(Color.LIGHT_GRAY);
        gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
        gOffScreen.setColor(c);
        paint(gOffScreen);
        g.drawImage(offScreenImage, 0, 0, null);
    }
    
    /**
     * Metode untuk meluncurkan frame game
     */
    public void launchFrame() {
        spawnEnemyTanks(); // Spawn enemy pertama kali
        
        setLocation(300, 100);
        setSize(GAME_WIDTH, GAME_HEIGHT);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        setResizable(false);
        setBackground(Color.LIGHT_GRAY);
        addKeyListener(new KeyMonitor());
        setVisible(true);
        
        // Gunakan thread dengan delay pendek agar animasi smooth
        new Thread(new PaintThread()).start();
    }
    
    public static void main(String[] args) {
        TankClient tc = new TankClient();
        tc.launchFrame();
    }
    
    private class PaintThread implements Runnable {
        public void run() {
            while (true) {
                repaint();
                try {
                    Thread.sleep(30);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    private class KeyMonitor extends KeyAdapter {
        public void keyPressed(KeyEvent e) {
            myTank.keyPressed(e);
        }
        public void keyReleased(KeyEvent e) {
            myTank.keyReleased(e);
        }
    }
}